package org.mozhu.mboot.admin.personal.notification.web.controller;

import org.mozhu.mboot.admin.AppUtils;
import org.mozhu.mboot.admin.maintain.notification.entity.Notification;
import org.mozhu.mboot.admin.maintain.notification.entity.NotificationModule;
import org.mozhu.mboot.admin.maintain.notification.service.NotificationService;
import org.mozhu.mboot.core.data.Searchable;
import org.mozhu.mboot.core.data.filter.Operator;
import org.mozhu.mboot.core.web.AjaxResponse;
import org.mozhu.mboot.core.web.bind.annotation.ControllerDefaults;
import org.mozhu.mboot.core.web.controller.BaseCRUDController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/personal/notification")
@Controller("personalNotificationController")
@ControllerDefaults(resourceIdentity = "personal:notification")
public class NotificationController extends BaseCRUDController<Notification, Long> {

    @Override
    protected void setCommonData(Model model) {
        super.setCommonData(model);
        model.addAttribute("modules", NotificationModule.values());
    }

    protected NotificationService getBaseService() {
        return (NotificationService) this.baseService;
    }

    @Override
    public Object list(HttpServletRequest request, Searchable searchable) {
        Long currentUserId = AppUtils.getCurrentUserId();
        searchable.addSearchFilter("userId", Operator.EQ, currentUserId);
        return super.list(request, searchable);
    }

    @RequestMapping(value = "/count")
    @ResponseBody
    public Object count() {
        return AjaxResponse.successWithData(this.getBaseService().countUnread(AppUtils.getCurrentUserId()));
    }

}